package com.heima.se.sjms.适配器;

// 目标接口
interface Target {
    void request();
}

// 适配者类   -- 需要被适配的类,例如：家庭电压：220V
class Adaptee {
    public int specificRequest() {
        System.out.println("家庭用电220V");
        return 220;
    }
}

// 类适配器  -- 手机适配器：将家庭电压适配为手机电压: 5V
class ClassAdapter extends Adaptee implements Target {
    @Override
    public void request() {
        int familyV = specificRequest();
        //适配
        int phoneV = familyV / 44;
        System.out.println("适配后给手机充电，电压："+ phoneV);
    }
}

// 对象适配器
class ObjectAdapter implements Target {
    private Adaptee adaptee;

    public ObjectAdapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}

public class AdapterPatternDemo {
    public static void main(String[] args) {
        // 类适配器示例
        Target classAdapter = new ClassAdapter();
        classAdapter.request();

        // 对象适配器示例
        Adaptee adaptee = new Adaptee();
        Target objectAdapter = new ObjectAdapter(adaptee);
        objectAdapter.request();
    }
}